﻿using System.Windows;
using IntelligentStorWpfApp.Model;
using System;
using IntelligentStorWpfApp.Controller;
using System.Collections.Generic;

namespace IntelligentStorWpfApp.View
{
    /// <summary>
    /// setting.xaml 的交互逻辑
    /// </summary>
    public partial class Setting : Window
    {
        public Setting()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }
        public ProductInfo productInfo{get;set;}
        private PositionController pc;
        private ProductController prodControl;
        private LinkProductPositionController linkPpcontrol;
        private void Setting_Load(object sender, RoutedEventArgs e)
        {
            txtProductId.Text = productInfo.ProductId;
            pc = new PositionController();
            prodControl = new ProductController();
            linkPpcontrol = new LinkProductPositionController();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            int cabinetNum,positionNum;
            if(int.TryParse(txtCabinet.Text,out cabinetNum)==false)
            {
                MessageBox.Show("请填入数字!");
                return;
            }
            if (int.TryParse(txtPosition.Text, out positionNum) == false)
            {
                MessageBox.Show("请填入数字!");
                return;
            }
            DateTime time = Convert.ToDateTime(tpStartDat.Text);
            if(DateTime.Compare(time,DateTime.Now)<0)
            {
                MessageBox.Show("时间不能早于当前时间!");
                return;
            }
            Position p = new Position();
            try
            {
                p.CabinetNum = cabinetNum;
                p.PositionNum = positionNum;
                int id = pc.getPostionId(p);
                if (id > -1)
                {
                    if (prodControl.checkPosiontionId(id))
                    {
                        MessageBox.Show("该位置已被占用!");
                        return;
                    }
                    else
                    {
                        LinkProductPosition linkPp = new LinkProductPosition();
                        linkPp.PositionId = id;
                        linkPp.ProductId = productInfo.ProductId;
                        linkPp.SetTime = time;
                        if (string.IsNullOrEmpty(productInfo.Pposition))
                        {
                            linkPp.Action = "Inport";
                        }
                        else
                        {
                            linkPp.Action = "Outport";
                        }
                        List<LinkProductPosition> listProdPosition = new List<LinkProductPosition>();
                        listProdPosition.Add(linkPp);
                        if (linkPpcontrol.addDraftLink(listProdPosition))
                        {
                            productInfo.Description = "设定在" + time.ToString("yyy-MM-dd HH:mm:ss") + " " + linkPp.Action;
                            prodControl.UdateProduct(productInfo);
                            MessageBox.Show("设定智能" + linkPp.Action + "成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("设定智能" + linkPp.Action + "失败!请稍后再试", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        
    }
}
